<?php

use think\facade\Db;

function add($model, $data)
{
    $model = is_string($model) ? Db::name($model) : $model;
    $add = $model->insert($data);
    $return = $add ? $add : false;
    return $return;
}

//编辑记录
function edit($model, $map, $data)
{
    $model = is_string($model) ? Db::name($model) : $model;
    $result = $model
        ->where($map)
        ->update($data);
    $return = $result === false ? false : true;
    return $return;

}

//删除记录
function doDelete($model, $map = array())
{
    $model = is_string($model) ? Db::name($model) : $model;
    $result = $model->where($map)
        ->delete();
    $return = $result == false ? false : true;
    return $return;
}

//获取简要记录
function simlist($model, $field = "*", $map = "", $orderby = '', $order = "DESC")
{
    $model = is_string($model) ? Db::name($model) : $model;
    $ostring = '';
    if (strpos($orderby, ';')) {
//多排序
        $sort_array = explode(';', $orderby);
        $kc = count($sort_array) - 1;
        foreach ($sort_array as $k => $value) {
            $va_array = explode(',', $value);
            $douhao = $kc == $k ? '' : ',';
            $va_string = $va_array[0] . ' ' . $va_array[1] . $douhao;
            $ostring .= $va_string;
        }
// die($ostring);

    } else {
//单排序
        if ($orderby !== "") {
            $ostring = $orderby . ' ' . $order;
        }

    }

    $data = $model
        ->field($field)
        ->where($map)
        ->order($ostring)
        ->select()->toArray();
    return $data;
}

//寻找单行记录的方法
function find($model, $field = '*', $map)
{
    $model = is_string($model) ? Db::name($model) : $model;
    $data = $model
        ->field($field)
        ->where($map)
        ->find();
    return $data;
}

//获取某记录中的某字段
function field($model, $map, $field)
{
    $model = is_string($model) ? Db::name($model) : $model;
    $item = $model
        ->where($map)
        ->column($field);
    if (empty($item)) return '';
    return $item[0];
}

//获取记录数的function
function getCount($model, $map)
{
    $model = is_string($model) ? Db::name($model) : $model;
    $count = $model
        ->where($map)
        ->count();
    return $count;
}

//获取记录数的function
function getSum($model, $map, $field)
{
    $model = is_string($model) ? Db::name($model) : $model;
    $count = $model
        ->where($map)
        ->sum($field);
    return $count;
}


function curlData($url, $data = [], $method = 'GET', $cookie = '', $headers = [])
{

    //初始化
    $ch = curl_init();

// 请求头，可以传数组
    curl_setopt($ch, CURLOPT_URL, $url);
//    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    if (!empty($cookie)) {
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);         // 执行后不直接打印出来
    if ($method == 'POST') {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');     // 请求方式
        curl_setopt($ch, CURLOPT_POST, true);               // post提交
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);              // post的变量
    }
    if ($method == 'PUT') {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    if ($method == 'DELETE') {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    if ($method == 'GET') {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
    $output = curl_exec($ch); //执行并获取HTML文档内容
    curl_close($ch); //释放curl句柄
    return $output;
}
function http_post_a($url, $data, $header = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    $response = curl_exec($ch);
//    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $response;
}





/**
 * 驼峰转换成下划线
 * @param $str
 * @return string
 */
function toUnderScore($str)
{
    $dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
        return '_' . strtolower($matchs[0]);
    }, $str);
    return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
}

/**
 * @param string $name
 * @param false $isLast
 * @return string
 */
function getMonthTable($name = '',$isLast = false)
{
    if($isLast){
        return $name . "_" . date('Y_m', time() - 3600*24*31);
    }else{
        return $name . "_" . date('Y_m', time());
    }
}

function addDebugLog($url,$param){
    if(is_array($param)){
        $param = json_encode($param);
    }
    add('app_debug_log',['url'=>$url,'request'=>$param]);
}





